<?php
namespace app\common\model;

use think\Db;
use think\Model;


class DrawCustom extends Model
{
    public function getTypeTextAttr($value,$data)
    {
        $type = [1=>'砸金蛋',6=>'开金宝箱',5=>'开银宝箱',4=>'开铜宝箱'];
        return $type[$data['a_type']];
    }

    public function getList(array $map = [])
    {
        return $this->alias('a')
        ->join([
            ['users u','a.user_id = u.user_id','left'],
            ['draw d','d.id = a.draw_id','left'],
        ])
        ->order('a.id desc')
        ->where($map)
        ->field('a.num,a.type as a_type,a.id as a_id,a.draw_id,a.status as a_status,a.create_time as c_time,a.update_time as u_time,u.nick_name,u.header_img,u.user_id,d.*')
        ->paginate('',false,['query'=>request()->param()])->each(function ($item){

                if ($item['type'] == 2){
                    $item['name'] = getCardName($item['key']);
                    $item['img']  = getCardImg($item['key']);
                }else{
                    $item['name'] = getGiftName($item['key']);
                    $item['img']  = getGiftImg($item['key']);
                }
        });

    }

    public function saveChange($data){
        if(is_numeric($data['id'])){
            return $this->allowField(true)->save($data,[$this->getPk()=>$data['id']]);
        }else{
            $this->allowField(true)->save($data);
        }
    }










}